package com.yhb.controller.admin;

import com.yhb.channel.FlowService;
import com.yhb.controller.admin.form.QueryExecutionFlowForm;
import com.yhb.controller.admin.vo.CommonFlowVo;
import com.yhb.controller.admin.vo.UnionPayFlowVo;
import com.yhb.model.RapidCheat;
import com.yhb.model.User;
import com.yhb.service.TradeService;
import com.yhb.service.UserService;
import com.yhb.utils.DateUtils;
import com.yhb.yinsheng.UnionPayService;
import lombok.var;
import me.edzh.spring.bootstrap.annotations.NeedAuth;
import me.edzh.spring.bootstrap.controller.BaseController;
import me.edzh.spring.bootstrap.model.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RequestMapping("/admin/flow")
@RestController

public class AdminFlowController extends BaseController {

    @Autowired
    private FlowService flowService;

    @Autowired
    private UnionPayService unionPayService;

    @Autowired
    private UserService userService;

    @Autowired
    private TradeService tradeService;

    @GetMapping("/{tradeId}")
    @NeedAuth(User.A_ADMIN)
    public Response getCommonFlow(@PathVariable("tradeId") String tradeId) {
        var trade = tradeService.rapidFlow(tradeId);
        var user = userService.getUser(trade.getUserId());

        var dateString = DateUtils.getDateString(trade.getCreateTime());

        if (trade.getType() == RapidCheat.Type.UNION_PAY_CHANNEL) {
            return Response.ok(UnionPayFlowVo.builder()
                    .user(user)
                    .consumeFlow(unionPayService.getFlow(trade.getConsumeFlowId(),dateString))
                    .remitFlow(unionPayService.getFlow(trade.getRemitFlowId(),dateString))
                    .build());
        } else {
            return Response.ok(CommonFlowVo.builder()
                    .user(user)
                    .consumeFlow(flowService.getFlowPrecisely(trade.getConsumeFlowId(),dateString))
                    .remitFlow(flowService.getFlowPrecisely(trade.getRemitFlowId(),dateString))
                    .build());
        }
    }

    @PostMapping("/query")
    @NeedAuth(User.A_ADMIN)
    public Response queryExcutionFlow(@RequestBody @Valid QueryExecutionFlowForm queryExecutionFlowForm){
        return Response.ok(flowService.getUnionPayFlow(queryExecutionFlowForm));

    }
}
